#include "BSP_Scene.h"
#include "Scene.h"

namespace FinalRendering
{
	class iScene;
	class iBspNode;
	using namespace FinalRendering::Engine;

	class BSP_Compiler : public Object
	{
	private:
		int BaseProg;
		int ProgSize;
		bool cancelled;
		Editor::Scene * scene;
		Array<RefPtr<Editor::Brush>> brushes;
		RefPtr<FinalRendering::Engine::Scene> bsp;
		void MergeBrushes();
		void CompileIntermediateScene(iScene * iscene);
		void BSP_Partition(iBspNode * node);
		void ComputePortals(Scene * scene);
		void ComputeVisibility(Scene * scene);
		bool TestPortalPortalVisibility(Scene * scene, Portal & p1, Portal & p2);
		void ComputeNodeVisiblity(Scene * scene, int node_id);
		bool TestPortalNodeVisibility(Scene * scene, int node_id, Portal & portal);
	public:
		Event<bool &> OnCheckCancel;
		Event<String, String> OnStatusChanged;
		Event<int, int> OnProgressChanged;
		BSP_Compiler(Editor::Scene * _scene);
		float PvsSamplingGrid;
		int MinSamples;
		bool ComputePVS;
		void Compile(const String & fileName);
		void CompilePhysics(const String & fileName);
	};
}